package com.codeoss.framework.db.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.module.ir.model.IrModel;
import com.codeoss.module.ir.model.IrModelAccessLog;
import com.codeoss.module.ir.service.IIrModelAccessLogService;
import com.codeoss.module.ir.service.IIrModelService;

@Component
public class ModelAccessLoggingUtil {
	@Autowired
	private IIrModelAccessLogService irModelAccessLogService;
	@Autowired
	private IIrModelService irModelService;
	@Transactional
	public void logDataSelect(YunContext yunContext,Table table) {
		IrModelAccessLog irModelAccessLog=new IrModelAccessLog();
		IrModel irModel=irModelService.getIrModelByTableName(table.getTableName());
		irModelAccessLog.setIrModelId(irModel.getId());
		irModelAccessLog.setAccessType("select");
		irModelAccessLogService.insert(irModelAccessLog);
	}
	@Transactional
	public void logDataUpdate(YunContext yunContext,Table table,Long recordId) {
		IrModelAccessLog irModelAccessLog=new IrModelAccessLog();
		IrModel irModel=irModelService.getIrModelByTableName(table.getTableName());
		irModelAccessLog.setIrModelId(irModel.getId());
		irModelAccessLog.setRecordId(recordId);
		irModelAccessLog.setAccessType("update");
		irModelAccessLogService.insert(irModelAccessLog);
	}
	@Transactional
	public void logDataInsert(YunContext yunContext,Table table,Long recordId) {
		IrModelAccessLog irModelAccessLog=new IrModelAccessLog();
		IrModel irModel=irModelService.getIrModelByTableName(table.getTableName());
		irModelAccessLog.setIrModelId(irModel.getId());
		irModelAccessLog.setRecordId(recordId);
		irModelAccessLog.setAccessType("insert");
		irModelAccessLogService.insert(irModelAccessLog);
	}
	@Transactional
	public void logDataDelete(YunContext yunContext,Table table) {
		IrModelAccessLog irModelAccessLog=new IrModelAccessLog();
		IrModel irModel=irModelService.getIrModelByTableName(table.getTableName());
		irModelAccessLog.setIrModelId(irModel.getId());
		irModelAccessLog.setAccessType("delete");
		irModelAccessLogService.insert(irModelAccessLog);
	}
}
